import { defineStore } from "pinia";
import { getCategoryList,getTotalNumber} from "@/api/category";

interface category1Data {
  id: number;
  showIndex: number;
  name: string;
  frontDesc: string;
  bannerUrl: string;
  imgUrl: string;
  level: string;
}
interface category2Data {
  id: number;
  superCategoryId: number;
  showIndex: number;
  name: string;
  bannerUrl: string;
  wapBannerUrl: string;
  type: number;
  categoryType: number;
  extra: {
    materialContentFrom: number;
    materialName: string;
    rcmdSort: boolean;
    taskType: number;
    itemFrom: number;
    crmUserGroupName: string;
    resourcesId: number;
    materialType: string;
    crmUserGroupId: number;
    materialId: number;
    taskId: number;
  };
}

interface categoryGroupData {
  id: number;
  name: string;
  categoryList: [
    {
      id: number;
      superCategoryId: number;
      showIndex: number;
      name: string;
      frontName: string;
      bannerUrl: string;
      wapBannerUrl: string;
      type: number;
      categoryType: number;
      extra: {
        materialContentFrom: number;
        materialName: string;
        rcmdSort: boolean;
        taskType: number;
        itemFrom: number;
        crmUserGroupName: string;
        resourcesId: number;
        materialType: string;
        crmUserGroupId: number;
        materialId: number;
        taskId: number;
      };
    }
  ];
}
interface subCate1ListData {
            id: number;
            superCategoryId: number;
            showIndex: number;
            name: string;
            frontName:string;
            frontDesc: string;
            level: string;
            wapBannerUrl: string;
            type: number;
            categoryType: number;
}
type currentCategoryData = {
  id: number | string;
  superCategoryId: number;
  showIndex: number;
  name: string;
  bannerList: [
    {
      id: number | string;
      picUrl: string;
      targetUrl: string;
      extra: {
        materialContentFrom: number;
        materialName: string;
        rcmdSort: boolean;
        taskType: number;
        itemFrom: number;
        resourcesId: number;
        materialType: string;
        crmUserGroupId: number;
        materialId: number;
        taskId: number;
      };
    }
  ];
};

type CategoryStoreStateData = {
  category1List: category1Data[];
  currentCategory: currentCategoryData;
  category2List: category2Data[];
  categoryGroupList: categoryGroupData[];
  subCate1List:subCate1ListData[];
  total:number|string
};

export const useCategoryStore = defineStore("category", {
  state: (): CategoryStoreStateData => {
    return {
      category1List: [],
      category2List: [],
      currentCategory: {
        id: "",
        superCategoryId: 0,
        showIndex: 0,
        name: "",
        bannerList: [
          {
            id: 0,
            picUrl: "",
            targetUrl: "",
            extra: {
              materialContentFrom: 0,
              materialName: "",
              rcmdSort: false,
              taskType: 0,
              itemFrom: 0,
              resourcesId: 0,
              materialType: "",
              crmUserGroupId: 0,
              materialId: 0,
              taskId: 0,
            },
          },
        ],
      },
      categoryGroupList: [],
      total:'',
      subCate1List: [],
    };
  },
  actions: {
    async getCategoryList(id: string | number) {
      const res = await getCategoryList(id);
      // console.log(res)
      this.category1List = res.data.categoryL1List;
      this.category2List = res.data.categoryL2List;
      this.currentCategory = res.data.currentCategory;
      this.categoryGroupList = res.data.categoryGroupList;
      // this.subCate1List = res.global.cateList.subCateList;
    },
    // 获取总商品总数
    async getTotalProject(){
        const res = await getTotalNumber()
        this.total = res.data
        console.log(res)
    }
  },
  getters: {
    picUrl: (state) => state.currentCategory.bannerList[0]?.picUrl || "",
  },
});
